package com.example.activiti.controller;

import com.example.activiti.common.core.controller.BaseController;
import com.example.activiti.common.core.domain.AjaxResult;
import com.example.activiti.common.core.page.PageDomain;
import com.example.activiti.common.core.page.TableDataInfo;
import com.example.activiti.common.core.page.TableSupport;
import com.example.activiti.domain.dto.ProcessDefinitionDTO;
import com.example.activiti.service.IProcessDefinitionService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 流程定义相关接口
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/processDefinition")
public class ProcessDefinitionController extends BaseController {

    @Resource
    private IProcessDefinitionService processDefinitionService;

    /**
     * 1.上传并部署流程定义
     *
     * @param file 流程图对象
     * @return 响应对象
     */
    @PostMapping(value = "/uploadStreamAndDeployment")
    public AjaxResult uploadStreamAndDeployment(@RequestParam("file") MultipartFile file) {
        processDefinitionService.uploadStreamAndDeployment(file);
        return AjaxResult.success();
    }

    /**
     * 通过stringBPMN添加流程定义
     *
     * @param stringBPMN
     * @return
     */
    @PostMapping(value = "/addDeploymentByString")
    public AjaxResult addDeploymentByString(@RequestParam("stringBPMN") String stringBPMN) {
        processDefinitionService.addDeploymentByString(stringBPMN);
        return AjaxResult.success();

    }

    /**
     * 2.获取流程定义集合
     *
     * @param processDefinition 筛选参数
     * @return 流程定义集合
     */
    @GetMapping(value = "/list")
    public TableDataInfo list(ProcessDefinitionDTO processDefinition) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        return getDataTable(processDefinitionService.selectProcessDefinitionList(processDefinition, pageDomain));
    }

    /**
     * 3.根据processDefinitionId激活或挂起流程流程定义
     *
     * @param processDefinition 参数
     * @return 响应对象
     */
    @PostMapping("/suspendOrActiveApply")
    @ResponseBody
    public AjaxResult suspendOrActiveApply(@RequestBody ProcessDefinitionDTO processDefinition) {
        processDefinitionService.suspendOrActiveApply(processDefinition.getId(), processDefinition.getSuspendState());
        return AjaxResult.success();
    }

    /**
     * 4.根据deploymentId删除流程定义
     *
     * @param deploymentId 部署ID
     * @return 响应对象
     */
    @DeleteMapping(value = "/remove/{deploymentId}")
    public AjaxResult delDefinition(@PathVariable("deploymentId") String deploymentId) {
        return toAjax(processDefinitionService.deleteProcessDefinitionById(deploymentId));
    }

    /**
     * 3.根据instanceId获取流程定义实例
     *
     * @param instanceId 实例ID
     * @return 响应对象
     */
    @GetMapping(value = "/getDefinitions/{instanceId}")
    public AjaxResult getDefinitionsByInstanceId(@PathVariable("instanceId") String instanceId) {
        return AjaxResult.success(processDefinitionService.getDefinitionsByInstanceId(instanceId));
    }

    /**
     * 4.根据deploymentId和resourceName获取流程定义XML
     *
     * @param response     响应
     * @param deploymentId 部署ID
     * @param resourceName 资源名称
     */
    @GetMapping(value = "/getDefinitionXml")
    public void getProcessDefineXml(HttpServletResponse response,
                                    @RequestParam("deploymentId") String deploymentId,
                                    @RequestParam("resourceName") String resourceName) {
        processDefinitionService.getProcessDefineXml(response, deploymentId, resourceName);
    }



}
